 package ga.wavaplayer.ui {

	import ga.wavaplayer.util.NumberUtil;
	import flash.display.Sprite;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldType;
	import flash.text.AntiAliasType;
		
	public class WTimeBox extends Sprite {
		
		private var _config:Object;
		private var _colors:Object;
		
		private var _tFormat:TextFormat;
		private var _tfCurrent:TextField;
		private var _tfTotal:TextField;
		
		private var totalHours:uint;
		private var totalMinutes:uint;
		private var totalSeconds:uint;
		
		public function WTimeBox(pConfig:Object, pColors:Object) {
			_config = pConfig;
			_colors = pColors;
			
			_tFormat = new TextFormat();
			_tFormat.font = 'Arial';
			_tFormat.size = 10;
			_tFormat.color = _colors.light;
			_tFormat.align = TextFormatAlign.LEFT;
			
			_tfTotal = new TextField();
			_tfTotal.autoSize = TextFieldAutoSize.LEFT;
			_tfTotal.defaultTextFormat = _tFormat;
			_tfTotal.antiAliasType = AntiAliasType.ADVANCED;
			
			_tfCurrent = new TextField();
			_tfCurrent.autoSize = TextFieldAutoSize.LEFT;
			_tfCurrent.defaultTextFormat = _tFormat;
			_tfCurrent.antiAliasType = AntiAliasType.ADVANCED;
			
			setTotalTime(0);
			setCurrentTime(0);
			
			this.addChild(_tfTotal);
			this.addChild(_tfCurrent);
		}
		
		public function setTotalTime(seconds:uint):void{
			
			totalHours = Math.floor(seconds / 3600);
			totalMinutes = Math.floor(seconds / 60) % 60;
			totalSeconds = seconds % 60;
			
			_tfTotal.text = _formatTime(seconds);
			_resize();
		}
		
		public function setCurrentTime(seconds:uint):void{
			_tfCurrent.text = _formatTime(seconds);
			_resize();
		}
		
		private function _formatTime(seconds:uint):String {
			var hours:uint = Math.floor(seconds / 3600);
			var minutes:uint = Math.floor(seconds / 60) % 60;
			var seconds:uint = seconds % 60;
			
			var str:String = '';
			
			if (totalHours > 0) {
				str += hours + ':';
			}
			if (totalMinutes > 0) {
				if (totalHours > 0) {
					str += NumberUtil.lead(minutes) + ':';
				}else{
					str += minutes + ':';
				}
			} else {
				str += '0:';
			}
			str += NumberUtil.lead(seconds, 2);
			
			return str;
		}
		
		private function _resize():void {
			
			var boxHeight:int = Math.floor(_config.height / 3) * 2;
			
			this.graphics.clear();
			this.graphics.beginFill(_colors.semilight);
			this.graphics.drawRect(0, Math.ceil(_config.height / 6), _tfCurrent.width + _tfTotal.width + 3, boxHeight);
			this.graphics.beginFill(_colors.dark);
			this.graphics.drawRect(1, Math.ceil(_config.height / 6) +1, _tfCurrent.width, boxHeight - 2);
			this.graphics.beginFill(_colors.semidark);
			this.graphics.drawRect(this.width - _tfTotal.width - 1, Math.ceil(_config.height / 6) +1, _tfTotal.width, boxHeight - 2);
			this.graphics.endFill();
			
			_tfCurrent.x = 1;
			
			_tfCurrent.y = 1;
			_tfTotal.x = this.width - _tfTotal.width - 1;
			_tfTotal.y = 1;
		}
	}
}
